<template>
  <a-card :body-style="{ height: 'calc(100vh - 125px)' }">
    <!-- 数据列表 -->
    <a-row
      class="w-full h-full flex flex-col overflow-x-auto overflow-y-hidden"
    >
      <!-- 查询条件 -->
      <a-row class="w-full" v-if="showSearchRow">
        <a-form
          :model="searchForm"
          layout="inline"
          label-align="left"
          size="small"
        >
          <a-form-item field="creatorName" label="操作人">
            <a-input v-model="searchForm.creatorName" placeholder="操作人" />
          </a-form-item>
          <a-form-item field="operatorLogName" label="操作名称">
            <a-input
              v-model="searchForm.operatorLogName"
              placeholder="操作名称"
            />
          </a-form-item>
          <a-form-item field="operatorLogMethod" label="请求方式">
            <a-select
              v-model="searchForm.operatorLogMethod"
              placeholder="请求方式"
              allow-clear
            >
              <a-option
                v-for="(d, index) in operatorLogMethod"
                :key="index"
                :value="d.dictKey"
                :label="d.dictValue"
              />
            </a-select>
          </a-form-item>
          <a-form-item field="status" label="响应状态">
            <a-select
              v-model="searchForm.status"
              placeholder="响应状态"
              allow-clear
            >
              <a-option
                v-for="(d, index) in dicts[proxy.DICT.commonNumberStatus]"
                :key="index"
                :value="d.dictKey"
                :label="d.dictValue"
              />
            </a-select>
          </a-form-item>
        </a-form>
        <a-row justify="center" class="w-full mt-2">
          <a-space>
            <a-button type="primary" @click="getPageList(false)">
              <template #icon><icon-search /></template>
              <template #default>查询</template>
            </a-button>
            <a-popconfirm
              content="确定删除选中操作日志吗?"
              type="warning"
              :popup-visible="deleteConfirmVisible"
              @ok="onDeleteConfirmOK"
              @cancel="onDeleteConfirmCancel"
            >
              <a-button
                type="primary"
                status="danger"
                @click="deleteBtnClick(selectedKeys)"
              >
                <template #icon><icon-delete /></template>
                <template #default>删除</template>
              </a-button>
            </a-popconfirm>
            <a-button @click="getPageList(true)">
              <template #icon><icon-sync /></template>
              <template #default>重置</template>
            </a-button>
          </a-space>
        </a-row>
      </a-row>

      <!-- 分割线 -->
      <a-divider v-if="showSearchRow" class="mt-2" />

      <!-- 数据操作区 -->
      <a-row class="w-full flex justify-between">
        <a-space>
          <!-- 添加 -->
        </a-space>
        <a-space>
          <!-- 刷新 -->
          <a-button shape="circle" size="small" @click="getPageList(false)">
            <template #icon><icon-refresh /></template>
          </a-button>
          <!-- 收缩/展开 -->
          <a-button
            shape="circle"
            size="small"
            @click="showSearchRow = !showSearchRow"
          >
            <template #icon>
              <icon-caret-up v-if="showSearchRow" />
              <icon-caret-down v-else />
            </template>
          </a-button>
        </a-space>
      </a-row>

      <!-- 数据展示区 -->
      <a-row class="w-full flex-1 mt-3 overflow-y-auto">
        <a-table
          class="w-[100%]"
          :scroll="{ minWidth: 600, y: '100%' }"
          :columns="datatable.columns"
          :data="datatable.records"
          :loading="datatable.loading"
          :pagination="false"
          table-layout-fixed
          row-key="operatorLogId"
          :row-selection="rowSelection"
          v-model:selectedKeys="selectedKeys"
        >
          <!-- 执行方法名称 -->
          <template #name="{ record }">
            <a-link @click="detailBtnClick(record.operatorLogId)" icon>{{
              record.name
            }}</a-link>
          </template>
          <!-- 响应状态 -->
          <template #status="{ record }">
            <dict-convert
              :dict-data="dicts[proxy.DICT.commonNumberStatus]"
              :dict-key="record.status"
            />
          </template>
          <!-- 操作 -->
          <template #operation="{ record }">
            <a-popconfirm
              content="确认要删除吗?"
              @ok="deleteBtnOkClick(record.operatorLogId)"
            >
              <a-button
                v-perm="['basic:operatorlog:delete']"
                type="text"
                status="danger"
                size="mini"
              >
                <template #icon>
                  <icon-delete />
                </template>
                <template #default>删除</template>
              </a-button>
            </a-popconfirm>
          </template>
        </a-table>
      </a-row>

      <!-- 分页 -->
      <a-row class="w-full flex justify-end mt-2">
        <pagination
          v-if="datatable.total > 0"
          v-model:page-num="searchForm.pageNum"
          v-model:page-size="searchForm.pageSize"
          :total="datatable.total"
          @pagination="getPageList(false)"
        />
      </a-row>
    </a-row>

    <a-modal
      v-model:visible="modal.visible"
      :esc-to-close="false"
      :mask-closable="false"
      draggable
      width="1000px"
      :footer="false"
    >
      <template #title>{{ modal.title }}</template>
      <component
        :is="modal.component"
        :params="modal.params"
        @ok="onOk"
        @cancel="onCancel"
        v-if="modal.visible"
      />
    </a-modal>
  </a-card>
</template>

<script setup>
import { ref, reactive, getCurrentInstance, shallowRef } from "vue";
import {
  getPageOperatorLogListApi,
  deleteOperatorLogByOperatorLogIdApi,
  deleteOperatorLogByOperatorLogIdsApi,
} from "~/api/operatorlog.js";
import OperatorLogDetail from "~/pages/basic/operatorlog/OperatorLogDetail.vue";

//全局实例
const { proxy } = getCurrentInstance();
//加载字典
const dicts = proxy.LoadDicts([proxy.DICT.commonNumberStatus]);
const operatorLogMethod = ref([
  {
    dictKey: "GET",
    dictValue: "GET",
  },
  {
    dictKey: "POST",
    dictValue: "POST",
  },
  {
    dictKey: "PUT",
    dictValue: "PUT",
  },
  {
    dictKey: "DELETE",
    dictValue: "DELETE",
  },
]);
//是否展示搜索区域
const showSearchRow = ref(true);
//搜索参数表单
const searchForm = reactive({
  //操作名称
  operatorLogName: null,
  //请求方式
  operatorLogMethod: null,
  //创建人
  creatorName: null,
  //响应状态
  status: null,
  //页码
  pageNum: 1,
  //条数
  pageSize: 10,
});
//表格选中数据
const selectedKeys = ref([]);
//表格选中
const rowSelection = reactive({
  type: "checkbox",
  showCheckedAll: true,
  onlyCurrent: true,
});
//数据列表
const datatable = reactive({
  //列配置
  columns: [
    {
      title: "操作名称",
      dataIndex: "name",
      slotName: "name",
      align: "center",
    },
    {
      title: "请求URI",
      dataIndex: "uri",
      align: "center",
    },
    {
      title: "请求方式",
      dataIndex: "method",
      align: "center",
      filterable: {
        filters: [
          {
            text: "GET",
            value: "GET",
          },
          {
            text: "POST",
            value: "POST",
          },
          {
            text: "PUT",
            value: "PUT",
          },
          {
            text: "DELETE",
            value: "DELETE",
          },
        ],
        filter: (value, record) => record.method == value,
        multiple: true,
      },
    },
    {
      title: "请求时间",
      dataIndex: "requestTime",
      align: "center",
      sortable: {
        sortDirections: ["ascend"],
      },
    },
    {
      title: "请求时长(ms)",
      dataIndex: "exceedTime",
      align: "center",
      sortable: {
        sortDirections: ["ascend"],
      },
    },
    {
      title: "响应状态",
      dataIndex: "status",
      slotName: "status",
      align: "center",
      filterable: {
        filters: [
          {
            text: "禁用",
            value: "0",
          },
          {
            text: "正常",
            value: "1",
          },
        ],
        filter: (value, record) => record.status == value,
        multiple: true,
      },
    },
    {
      title: "操作人",
      dataIndex: "creatorName",
      align: "center",
    },
    {
      title: "操作",
      slotName: "operation",
      align: "center",
      width: 160,
      fixed: "right",
    },
  ],
  //加载
  loading: false,
  //数据列表
  records: [],
  //总条数
  total: 0,
});
//查询数据列表
const getPageList = (isReset = false) => {
  if (isReset) {
    searchForm.operatorLogName = null;
    searchForm.operatorLogMethod = null;
    searchForm.creatorName = null;
    searchForm.status = null;
    searchForm.pageNum = 1;
    searchForm.pageSize = 10;
  }
  datatable.loading = true;
  getPageOperatorLogListApi(searchForm)
    .then((res) => {
      datatable.records = res.records;
      datatable.total = res.total;
    })
    .finally(() => {
      datatable.loading = false;
    });
};
//公共模态框
const modal = reactive({
  //是否显示
  visible: false,
  //模态框标题
  title: "操作日志管理",
  //传递参数
  params: {},
  //组件名称
  component: null,
});
//表格行数据 "查看" -> 点击
const detailBtnClick = (operatorLogId) => {
  console.log("operatorLogId", operatorLogId);
  modal.visible = true;
  modal.title = "操作日志详细信息";
  modal.params = {
    operationType: proxy.operationType.detail.type,
    operatorLogId: operatorLogId,
  };
  modal.component = shallowRef(OperatorLogDetail);
};
//删除确认弹窗
const deleteConfirmVisible = ref(false);
//表格行数据 "删除" -> 点击
const deleteBtnClick = (selectedKeys) => {
  if (selectedKeys.length === 0) {
    proxy.$msg.error("请选择要删除的操作日志");
    return;
  }
  deleteConfirmVisible.value = true;
};
//表格行数据 "删除" -> 确认
const deleteBtnOkClick = (operatorLogId) => {
  deleteOperatorLogByOperatorLogIdApi(operatorLogId).then(() => {
    proxy.$msg.success(proxy.operationType.delete.success);
    //刷新列表
    getPageList();
  });
};
const onDeleteConfirmCancel = () => {
  deleteConfirmVisible.value = false;
  selectedKeys.value = [];
};
//删除确认弹窗 -> 确认
const onDeleteConfirmOK = () => {
  deleteConfirmVisible.value = false;
  let userIds = [];
  selectedKeys.value.map((userId) => {
    userIds.push(userId);
  });
  deleteOperatorLogByOperatorLogIdsApi(userIds).then(() => {
    proxy.$msg.success(proxy.operationType.delete.success);
    //刷新列表
    selectedKeys.value = [];
    getPageList();
  });
};

const onOk = () => {
  modal.visible = false;
  //查询数据列表
  getPageList();
};
//模态框 -> 取消
const onCancel = () => {
  modal.visible = false;
};

//查询数据列表
getPageList();
</script>
<style scoped></style>
